package cn.txm.product.Controller;

import cn.txm.basic.util.AjaxResult;
import cn.txm.basic.util.PageList;
import cn.txm.basic.util.RedisUtils;
import cn.txm.org.domain.Employee;
import cn.txm.product.domain.Product;
import cn.txm.product.query.ProductQuery;
import cn.txm.product.service.IProductService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品、服务项目接口
 * @author Administrator
 * @version 1.0
 * @date 2021/1/17 11:37
 */
@RestController
public class ProductController {
    @Autowired
    private IProductService productService;

    /**
     * 分页查询商品列表 + 支持高级查询条件
     * @param productQuery
     * @return
     */
    @PostMapping("/product")
    public PageList<Product> pageList(@RequestBody ProductQuery productQuery, HttpServletRequest request){
        //获取等钱登录的商机用户，获取到shop_id
        Employee employee =RedisUtils.INSTANCE.getUser(request, Employee.class);
        productQuery.setShop_id(employee.getShop().getId());
        return productService.queryPage(productQuery);
    }


    /**
     * 新增/修改商品信息
     * @param product
     * @return
     */
    @PutMapping("/product")
    public AjaxResult save(@RequestBody Product product, HttpServletRequest request){
        try {
            //获取等钱登录的商机用户，获取到shop_id
            Employee employee =RedisUtils.INSTANCE.getUser(request, Employee.class);
            product.setShop_id(employee.getShop().getId());
            System.out.println(product);
            productService.saveOrUpdate(product);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }

    /**
     * 删除商品和商品详情
     * @param id
     * @return
     */
    @DeleteMapping("/deleteproduct/{id}")
    public AjaxResult deleteproductAnddetail(@PathVariable Long id){
        System.out.println(id);
        try {
            productService.deleteproductAnddetail(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }

    /**
     * 批量上架
     * @param ids
     * @return
     */
    @PatchMapping("/batchshelves")
    public AjaxResult batchshelves(@RequestBody List<Long> ids){
        try {
            productService.batchshelves(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*链式编程思想*/
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙！"+e.getMessage());
        }
    }
    /**
     * 批量下架
     * @param ids
     * @return
     */
    @PatchMapping("/batchnoshelves")
    public AjaxResult batchnoshelves(@RequestBody List<Long> ids){
        try {
            productService.batchnoshelves(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*链式编程思想*/
            return AjaxResult.me().setSuccess(false)
                    .setMessage("系统繁忙！"+e.getMessage());
        }
    }
}
